package client.view;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;

/**
 * Dragger is a MouseAdapter that allows the JComponent it is associated with
 * to be dragged in its parent container.
 */
public class Dragger extends MouseAdapter implements MouseMotionListener {
	private Point dragPoint;	// the point on the component at which the dragging started
	private boolean ctrlDrag;
	
	/**
	 * Creates a new Dragger to move a component in its container
	 * @param ctrlDrag only drag the component if the Ctrl key is held down
	 */
	public Dragger(boolean ctrlDrag) {
		this.ctrlDrag = ctrlDrag;
	}

	public void mouseDragged(MouseEvent e) {
		if (ctrlDrag == ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0)) {
			JComponent source = ((JComponent)e.getSource());
			source.setLocation(source.getLocation().x + e.getX() - dragPoint.x, source.getLocation().y + e.getY() - dragPoint.y);
		}
	}

	public void mouseReleased(MouseEvent e) {
		((JComponent)e.getSource()).revalidate();
	}

	public void mouseMoved(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		dragPoint = e.getPoint();
	}
}